#include "instrument.h"
#include <stdlib.h>
#include <stdio.h>

int main()
{
  char* jsonContent=FileRead("/home/max/Documents/vuln/conf/monitors.conf");
  cJSON * jp=jsonParser(jsonContent);

  char *monitorid[2]={NULL};
  char *line0line1[2]={NULL};
  int index=0;
  char *id=NULL;
  cJSON *details=NULL;

  char *filepath=NULL;
  char *line=NULL;
  int line0,line1;
  char* target=NULL;
  char* value=NULL;
  char* funcname=NULL;
  // char* value=NULL;
  char*type=NULL;
  char *frequency=NULL;

  for(int i=0; i<cJSON_GetArraySize(jp); i++)   //遍历最外层json键值对
    {
        cJSON * item = cJSON_GetArrayItem(jp, i);        
        if(cJSON_Object == item->type)      //如果对应键的值仍为cJSON_Object就递归调用printJson
        {
          // printJson(item);
          monitorid[0]=monitorid[1]=NULL;
          line0line1[0]=line0line1[1]=NULL;
          index=0;
          details=getJsonObj(item,"details");
          id=cJSON_Print(getJsonObj(cJSON_GetArrayItem(details,0),"id"));
          printf("%s",id);

          splitid(id,monitorid);
          printf("%s  %d\n",monitorid,atoi(monitorid+0));
          printf("%s  %d\n",monitorid+1, atoi(monitorid+1));

          switch (atoi(monitorid+0))
          {
           case 0:
            
            filepath=cJSON_Print(getJsonObj(item,"path"));

            if(atoi(monitorid+1)==0)
            {
              //find  the function in the target file.
              line=cJSON_Print(getJsonObj(item,"line"));
              funcname=cJSON_Print(getJsonObj(item,"target"));
            }
            else
            {
              //find  the code fragment in the target file.
              target=cJSON_Print(getJsonObj(item,"target"));
              splitid(target,line0line1);
              printf("%s  %d\n",line0line1,atoi(line0line1+0));
              printf("%s  %d\n",line0line1+1, atoi(line0line1+1));
            }

            value=cJSON_Print(getJsonObj(cJSON_GetArrayItem(details,0),"value"));

            // uuid 
            // modify the code 

            break;
          
          case 1:
            // filepath=cJSON_Print(getJsonObj(item,"path"));
            type=cJSON_Print(getJsonObj(item,"type"));
            target=cJSON_Print(getJsonObj(item,"target"));
            value=cJSON_Print(getJsonObj(cJSON_GetArrayItem(details,0),"value"));

            // CPU monitor
            if(atoi(monitorid+1)==0)
            {
              if(strcmp(type,"pid")==0)
              {
                    // append and monitor
              }
              else
              {
                    // launch and monitor
              }
                
              
            }
            // Mem monitor
            else
            {
              if(strcmp(type,"pid")==0)
              {
                    // append and monitor
              }
              else
              {
                    // launch and monitor
              }
            }

            break;
          
          case 2:
            
            type=cJSON_Print(getJsonObj(item,"type"));
            target=cJSON_Print(getJsonObj(item,"target"));
            frequency=cJSON_Print(getJsonObj(cJSON_GetArrayItem(details,0),"frequency"));

            // code segment monitor
            if(atoi(monitorid+1)==0)
            {
              if(strcmp(type,"pid")==0)
              {
                //target=pid
                // append and monitor
              }
              else
              {
                // target=program path
                // launch and monitor
              }
                
              
            }
            // data file  monitor
            else
            {
              filepath=cJSON_Print(getJsonObj(item,"path"));
              // target=lines in filepath
              splitid(target,line0line1);
              printf("%s  %d\n",line0line1,atoi(line0line1+0));
              printf("%s  %d\n",line0line1+1, atoi(line0line1+1));
            }
            break;

          case 3:
            filepath=cJSON_Print(getJsonObj(item,"path"));
            line=cJSON_Print(getJsonObj(item,"line"));
            target=cJSON_Print(getJsonObj(item,"target"));
            value=cJSON_Print(getJsonObj(cJSON_GetArrayItem(details,0),"value"));

            if(atoi(monitorid+1)==0)  //variable range
            {
              // 1-12,2-23
            }
            else
            {
              //1->12,1->23,23->43
            }

            break;

          case 4:
            filepath=cJSON_Print(getJsonObj(item,"path"));
            target=cJSON_Print(getJsonObj(item,"target"));
           

            // find the function,insert the code at the begin and end of the function.
            break;
          case 5:
             target=cJSON_Print(getJsonObj(item,"target"));

            break;
          
          default:
            break;
          }

        }  

    }
  // printJson(jp);
  
  
return 0;
}